package com.sikaryofficial.backend.service.brand.export;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.sikaryofficial.common.core.domain.R;
import com.sikaryofficial.common.core.utils.SpringUtils;
import com.sikaryofficial.system.api.RemoteFileService;
import com.sikaryofficial.system.api.domain.SysFile;
import lombok.NoArgsConstructor;

import java.io.File;
import java.util.Objects;

import static com.sikaryofficial.common.core.utils.file.FileUtils.fileToMultipartFile;

/**
 * @author : qinjinyuan
 * @desc :  导入图片转地址
 * @date : 2023/11/29 15:02
 */
@NoArgsConstructor
public class ProductImageConverter implements Converter<File> {

    @Override
    public Class<?> supportJavaTypeKey() {
        return File.class;
    }

    @Override
    public WriteCellData<?> convertToExcelData(File value, ExcelContentProperty contentProperty,
                                               GlobalConfiguration globalConfiguration)  {

        RemoteFileService  remoteFileService = SpringUtils.getBean(RemoteFileService.class);
        R<SysFile> sysFileR = remoteFileService.upload(fileToMultipartFile(value));
        if(Objects.isNull(sysFileR) || Objects.isNull(sysFileR.getData())){
            return new WriteCellData<>("");
        }
        return new WriteCellData(sysFileR.getData().getUrl());
    }

//    @Override
//    public CellData convertToExcelData(File value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
//        RemoteFileService  remoteFileService = SpringUtils.getBean(RemoteFileService.class);
//        R<SysFile> sysFileR = remoteFileService.upload(fileToMultipartFile(value));
//        if(Objects.isNull(sysFileR) || Objects.isNull(sysFileR.getData())){
//            return new CellData("");
//        }
//        return new CellData(sysFileR.getData().getUrl());
//    }

}
